#include "edge.hpp"
#include <algorithm>

#define INVALID_NODE_ID                 ( -1 )
#define INVALID_EDGE_PROBABILITY        ( -1.0 )

edge_c::~edge_c ( void )
{
}


edge_c::edge_c ( void ) : 
        first_node_id  ( INVALID_NODE_ID ), 
        second_node_id ( INVALID_NODE_ID ),
        probability    ( INVALID_EDGE_PROBABILITY )
{
}

edge_c::edge_c ( long first_node_id, long second_node_id, double probability ) :
        first_node_id   ( first_node_id ),
        second_node_id  ( second_node_id ),
        probability     ( probability ) 
{
}

long edge_c::get_first_node_id ( void ) const
{
        return first_node_id;
}

long edge_c::get_second_node_id ( void ) const
{
        return second_node_id;
}

double edge_c::get_probability ( void ) const
{
        return probability;
}
        
bool edge_c::is_rigid ( void ) const
{
        return probability >= 1.0;
}

bool edge_c::is_valid ( void ) const
{
        return first_node_id >= 0 && 
               second_node_id >= 0 && 
               first_node_id != second_node_id &&
               probability >= 0.0 &&
               probability <= 1.0;
}

bool edge_c::operator== ( const edge_c & right ) const
{
        if ( ( first_node_id == right.first_node_id && second_node_id == right.second_node_id )
             || ( first_node_id == right.second_node_id && second_node_id == right.first_node_id ) )
                return true;
        else
                return false; 
}

bool edge_c::is_normalized ( void ) const
{
        return first_node_id < second_node_id;
}

void edge_c::normalize ( void )
{
        if ( ! is_normalized () )
                std::swap ( first_node_id, second_node_id );
}

void edge_c::parse ( std::istream & stream, bool parse_probability )
{
        stream >> first_node_id >> second_node_id;
        if ( parse_probability )
        {
                stream >> probability;
        }
        else
        {
                probability = 1.0;
        }
}

